package com.zyh.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

import java.io.IOException;

public class HBaseTest {
    @Test
    public void testNamespace() throws Exception{

        //1 读取配置文件
        Configuration conf = HBaseConfiguration.create();
        conf.addResource("hbase-site.xml");

        //2 根据读取到的配置,获取链接
        Connection conn = ConnectionFactory.createConnection(conf);

        //        3. 通过Connection获取执行命令的工具
        Admin admin = conn.getAdmin();

        //        4. 设置命令需要的参数
        NamespaceDescriptor baizhins = NamespaceDescriptor.create("baizhins").build();

        //        5. 执行命令
        admin.createNamespace(baizhins);
        //        admin.deleteNamespace("baizhins"); 删除命令空间

        //        6. 释放资源
        admin.close();
        conn.close();
    }

    @Test
    public void testTable() throws Exception{
        //1 读取配置文件
        Configuration conf = HBaseConfiguration.create();
        conf.addResource("hbase-site.xml");

        //2 根据读取到的配置,获取链接
        Connection conn = ConnectionFactory.createConnection(conf);

        //        3. 通过Connection获取执行命令的工具
        Admin admin = conn.getAdmin();

/*      创建表
        TableName tableName = TableName.valueOf("baizhi", "t_student");
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName)
                .addFamily(new HColumnDescriptor("info"))
                .addFamily(new HColumnDescriptor("ship"));

        admin.createTable(hTableDescriptor);*/
        //禁用,删除表
        admin.disableTable(TableName.valueOf("baizhi:t_user"));
        admin.deleteTable(TableName.valueOf("baizhi:t_user"));
        //        6. 释放资源
        admin.close();
        conn.close();
    }

    @Test
    public void testPut() throws Exception{
        //        1. 使用Configuration读取配置文件
        Configuration conf = HBaseConfiguration.create();
        conf.addResource(ClassLoader.getSystemResourceAsStream("hbase-site.xml"));

//        2. 根据Configuration中的信息创建Connection（链接）
        Connection conn = ConnectionFactory.createConnection(conf);

//        3. 通过Connection获取执行命令的工具,操作表增删改查的工具table
        Table table = conn.getTable(TableName.valueOf("baizhi", "t_student"));

//        4. 设置命令需要的参数
        Put put = new Put(Bytes.toBytes("0001"))
                .addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("xiaohei"))
                .addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(18))
                .addColumn(Bytes.toBytes("ship"),Bytes.toBytes("college"),Bytes.toBytes("郑州大学"))
                .addColumn(Bytes.toBytes("ship"),Bytes.toBytes("major"),Bytes.toBytes("发展心理学"));

//        5. 执行命令
        table.put(put);

//        6. 释放资源
        table.close();
        conn.close();
    }

    @Test
    public void testRead() throws IOException {
//        1. 使用Configuration读取配置文件
        Configuration conf = HBaseConfiguration.create();
        conf.addResource(ClassLoader.getSystemResourceAsStream("hbase-site.xml"));

//        2. 根据Configuration中的信息创建Connection（链接）
        Connection conn = ConnectionFactory.createConnection(conf);

//        3. 通过Connection获取执行命令的工具
        Table table = conn.getTable(TableName.valueOf("baizhi", "t_student"));

//        4. 设置命令需要的参数
        Get get = new Get(Bytes.toBytes("0001"))
                .setMaxVersions(3);//最多3个版本的数据

//        5. 执行命令
        Result result = table.get(get);
        byte[] nameValueBytes = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
        byte[] ageValueBytes = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));
        byte[] collegeValueBytes = result.getValue(Bytes.toBytes("ship"), Bytes.toBytes("college"));
        byte[] majorValueBytes = result.getValue(Bytes.toBytes("ship"), Bytes.toBytes("major"));
        byte[] rowKeyBytes = result.getRow();

        System.out.println(Bytes.toString(rowKeyBytes));
        System.out.println(Bytes.toString(nameValueBytes));
        System.out.println(Bytes.toInt(ageValueBytes));
        System.out.println(Bytes.toString(collegeValueBytes));
        System.out.println(Bytes.toString(majorValueBytes));

//        6. 释放资源
        table.close();
        conn.close();
    }

    // 删除数据，对应的命令： delete "baizhi:t_student","0001","ship:major"
    @Test
    public void testDelete() throws IOException {
//        1. 使用Configuration读取配置文件
        Configuration conf = HBaseConfiguration.create();
        conf.addResource(ClassLoader.getSystemResourceAsStream("hbase-site.xml"));

//        2. 根据Configuration中的信息创建Connection（链接）
        Connection conn = ConnectionFactory.createConnection(conf);

//        3. 通过Connection获取执行命令的工具
        Table table = conn.getTable(TableName.valueOf("baizhi", "t_student"));

//        4. 设置命令需要的参数
        Delete delete = new Delete(Bytes.toBytes("0001"))
                .addColumn(Bytes.toBytes("ship"), Bytes.toBytes("major"));

//        5. 执行命令
        table.delete(delete);

//        6. 释放资源
        table.close();
        conn.close();
    }

    // 查询多条数据，对应的命令 scan "baizhi:t_student",{COLUMNS=>["info","ship"],STARTROW=>"0001",LIMIT=>2}
    @Test
    public void testScan() throws IOException {
//        1. 使用Configuration读取配置文件
        Configuration conf = HBaseConfiguration.create();

//        2. 根据Configuration中的信息创建Connection（链接）
        Connection conn = ConnectionFactory.createConnection(conf);

//        3. 通过Connection获取执行命令的工具
        Table table = conn.getTable(TableName.valueOf("baizhi", "t_student"));

//        4. 设置命令需要的参数
        Scan scan = new Scan(Bytes.toBytes("0001"))
                .addFamily(Bytes.toBytes("info"))
                .addFamily(Bytes.toBytes("ship"))
                .setFilter(new PageFilter(2));//返回前两行

//        5. 执行命令
        ResultScanner rs = table.getScanner(scan);
        for (Result result : rs) {
            byte[] nameValueBytes = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
            byte[] ageValueBytes = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));
            byte[] collegeValueBytes = result.getValue(Bytes.toBytes("ship"), Bytes.toBytes("college"));
            byte[] majorValueBytes = result.getValue(Bytes.toBytes("ship"), Bytes.toBytes("major"));
            byte[] rowKeyBytes = result.getRow();

            System.out.println(Bytes.toString(rowKeyBytes));
            System.out.println(Bytes.toString(nameValueBytes));
            System.out.println(Bytes.toInt(ageValueBytes));
            System.out.println(Bytes.toString(collegeValueBytes));
            System.out.println(Bytes.toString(majorValueBytes));
        }

//        6. 释放资源
        rs.close();
        table.close();
        conn.close();
    }
}
